package top.infopub.mgr.mkt.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.dto.mkt.CommonCreditDetailsDto;
import top.infopub.enums.mkt.BackMechEnum;
import top.infopub.enums.mkt.ProjectFileTypeEnum;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.mkt.dao.CreditDetailsDtoDao;
import top.infopub.mgr.mkt.dao.ProjectAttachmentDao;
import top.infopub.mgr.mkt.service.CreditDetailsDtoService;
import top.infopub.model.mkt.BidReview;
import top.infopub.model.mkt.CreditDetail;
import top.infopub.model.mkt.ProjectAttachment;
import top.infopub.model.mkt.ProjectInfo;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author Warking
 * @Date 2021/5/5 11:11
 * @Version 1.0
 */
@Service
public class CreditDetailsDtoServiceImpl extends GenericFlowController implements CreditDetailsDtoService {
    @Autowired
    private CreditDetailsDtoDao creditDetailsDtoDao;
    @Autowired
    private ProjectAttachmentDao projectAttachmentDao;
    @Autowired
    private CollectProjectServiceImpl collectProjectService;

    @Override
    public CommonCreditDetailsDto queryDetail(String proCode) {
        //调用公用方法
        CommonCreditDetailsDto dto = commonTender(new CommonCreditDetailsDto(), proCode, "1");

        List<CreditDetail> creditDetailList = creditDetailsDtoDao.creditDetail(proCode);
        dto.setCreditDetail(creditDetailList);
        //附件信息表
        List<ProjectAttachment> projectAttaList = projectAttachmentDao.queryByWhere(proCode, ProjectFileTypeEnum.FILE_TYPE24.getCode(), null);
        dto.setFileAtt(collectProjectService.fileAttaListToArray(projectAttaList));
        return dto;
    }

    //公共的标书方法
    public CommonCreditDetailsDto commonTender(CommonCreditDetailsDto dto, String proCode, String bidType) {
        List<Map<String, Object>> lists = creditDetailsDtoDao.queryDetail(proCode, bidType);
        Map<String, Object> maps = null;
        if (lists != null && lists.size() > 0) {
            maps = lists.get(0);
        }
        ProjectInfo projectInfo = new ProjectInfo();
        BidReview bidReview = new BidReview();
        //set到ProjectInfo
        if (maps != null) {
            projectInfo.setId((String) maps.get("SGUID"));
            projectInfo.setProName((String) maps.get("PRO_NAME"));
            projectInfo.setProCode((String) maps.get("PRO_CODE"));
            projectInfo.setCostCae((BigDecimal) maps.get("COST_CAE"));//建安工程费用
            String s = BackMechEnum.getByCode((String) maps.get("BACK_MECH")).getName();
            if ("1".equals(s) || "2".equals(s) || "3".equals(s)) {
                projectInfo.setBackMech("数据异常");//回报机制
            } else {
                projectInfo.setBackMech(s);
            }//回报机制
            projectInfo.setCompName((String) maps.get("COMP_NAME"));
            projectInfo.setOmYears((BigDecimal) maps.get("OM_YEARS"));
            projectInfo.setStaffName((String) maps.get("STAFF_NAME"));
            projectInfo.setSumAmount((BigDecimal) maps.get("SUM_AMOUNT"));
            projectInfo.setBuildYears((BigDecimal) maps.get("BUILD_YEARS"));
            projectInfo.setProRemarks((String) maps.get("PRO_REMARKS"));
            projectInfo.setCarryOrg((String) maps.get("COOP_NAME")); //实施机构

            bidReview.setInvestEndTime((Date) maps.get("INVEST_END_TIME"));
            bidReview.setWillApplyMeetTime((Date) maps.get("WILL_APPLY_MEET_TIME"));
            bidReview.setProManagers((String) maps.get("PRO_MANAGERS"));
            bidReview.setId((String) maps.get("ID"));
        }
        //获取用户
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        projectInfo.setProposer(vo.getHrStaffId());
        //根据申请人ID查询申请公司
        String compName = creditDetailsDtoDao.getApplicant(vo.getHrStaffId());
        projectInfo.setApplicant(compName);
        //set到dto
        dto.setBidReview(bidReview);
        dto.setProjectInfo(projectInfo);

        return dto;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(CommonCreditDetailsDto dto, String proCode) {
        //获取用户
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        //修改 标书评审的数据
        BidReview bidReview = dto.getBidReview();
        if (bidReview.getInvestEndTime() != null || bidReview.getWillApplyMeetTime() != null || (bidReview.getProManagers() != null && bidReview.getProManagers() != "")) {
            /*Review需要有项目全局唯一编码 */
            bidReview.setProCode(proCode);
            creditDetailsDtoDao.updateReview(bidReview);
        }
        //修改和保存资信标数据
        List<CreditDetail> creditDetailList = dto.getCreditDetail();
        if (creditDetailList != null && creditDetailList.size() > 0) {
            for (int i = 0; i < creditDetailList.size(); i++) {
                CreditDetail creditDetail = creditDetailList.get(i);
                if (creditDetail.getId() != null && creditDetail.getId() != "") {
                    //修改
                    creditDetail.setUptBy(vo.getId());
                    creditDetailsDtoDao.updateSingle(creditDetail);

                } else {
                    //保存
                    creditDetail.setAddBy(vo.getId());
                    creditDetail.setUptBy(vo.getId());
                    creditDetail.setProCode(proCode);
                    creditDetail.setBidEvalId(bidReview.getId());
                    creditDetailsDtoDao.insertSingle(creditDetail);
                }
            }//for
        }// if


        //更新附件表-MKT_PROJECT_ATTACHMENT
        List<ProjectAttachment> attachmentList = new ArrayList<>();
        if (dto.getFileAtt() != null) {
            String[] fileAtt = dto.getFileAtt();
            for (String file : fileAtt) {
                ProjectAttachment attachment = createProjectFileAtta(vo, proCode, file);
                attachmentList.add(attachment);
            }
        }
        //更新附件信息
        projectAttachmentDao.deleteByWhere(proCode, ProjectFileTypeEnum.FILE_TYPE24.getCode(), null);
        if (attachmentList != null && attachmentList.size() > 0) {
            projectAttachmentDao.insertBatch(attachmentList);
        }
        return 1;
    }

    @Override
    public int deleteCreditDetail(String sguid) {
        return creditDetailsDtoDao.deleteCreditDetail(sguid);

    }

    //根据ID查询标书评审的审批状态
    @Override
    public String isFlag(String sguid) {
        return creditDetailsDtoDao.isFlag(sguid);
    }

    @Override
    public BidReview selectByPrimaryKey(String id) {
        return creditDetailsDtoDao.selectByPrimaryKey(id);
    }
}
